package app.beit.finportal.controller.backdoor;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import app.beit.finportal.bean.AccountBean;
import app.beit.finportal.bean.SlidesBean;
import app.beit.finportal.controller.BaseController;
import app.beit.finportal.controller.FintrackResponse;
import app.beit.finportal.service.SlidesService;
import app.beit.finportal.utils.constr.MsgConstr;
import app.beit.finportal.utils.constr.SessionConstr;

@Controller
@RequestMapping("/backend/slides")
public class BackdoorSlidesController extends BaseController {
	
	@Autowired
	private SlidesService slidesService;
	
	@RequestMapping
	public String main(final HttpServletRequest request){
		super.removeSessionNotUser(request);
		request.setAttribute(SessionConstr.ACTIVE_PAGE, "slides");
		return "backend.slides";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="getAll", method=RequestMethod.POST)
	@ResponseBody
	public FintrackResponse<List<SlidesBean>> getAll(final HttpServletRequest request){
		FintrackResponse<List<SlidesBean>> response = null;
		try{
			final List<SlidesBean> beans = this.slidesService.getAllBackdoor();
			if(beans != null && beans.size() > 0){
				final Map<Long, SlidesBean> maps = new HashMap<Long, SlidesBean>();
				for(final SlidesBean bean : beans){
					maps.put(bean.getId(), bean);
				}
				request.getSession().setAttribute(SessionConstr.NEWS_ALL_DATAS, maps);
				response = super.responseSuccess();
				response.setDatas(beans);
			}else{
				response = super.responseSuccessNullData();
			}
		}catch(Exception e){
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
			e.printStackTrace();
		}
		return response;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="addOrUpdate", method = RequestMethod.POST)
	public String addOrUpdate(final HttpServletRequest request, final @RequestParam("file") MultipartFile file, final @RequestParam("description") String description, 
			final @RequestParam("url") String url, final @RequestParam(value="status", required=false) boolean status, final @RequestParam("newsId") String newsId, 
			final @RequestParam("newsOrder") String newsOrder, final @RequestParam("name") String name){
		FintrackResponse<SlidesBean> response = null;
		try{			
			final AccountBean account = (AccountBean) request.getSession().getAttribute(SessionConstr.USER);
			System.err.println("********************************* "+name);
			if(newsOrder.equalsIgnoreCase("ADD")){
				final SlidesBean bean = new SlidesBean();
				bean.setCreatedBy(account.getUsername());
				bean.setCreatedDate(new Date(System.currentTimeMillis()));
				bean.setUpdatedBy(account.getUsername());
				bean.setUpdatedDate(new Date(System.currentTimeMillis()));
				
				bean.setDescription(description);
				bean.setStatus(status ? 1 : 0);
				bean.setUrl(url);
				bean.setPictureLocation(file.getOriginalFilename());
				bean.setName(name);
				
				byte[] b = file.getBytes();
				bean.setPicture(b);
				
				this.slidesService.add(bean);
				response = super.responseSuccess();
				response.setDatas(bean);
			}else if(newsOrder.equalsIgnoreCase("UPDATE")){
				final Map<Long, SlidesBean> maps = (Map<Long, SlidesBean>) request.getSession().getAttribute(SessionConstr.NEWS_ALL_DATAS);
				final SlidesBean bean = maps.get(Long.parseLong(newsId));
				bean.setUpdatedBy(account.getUsername());
				bean.setUpdatedDate(new Date(System.currentTimeMillis()));
				
				bean.setDescription(description);
				bean.setStatus(status ? 1 : 0);
				bean.setUrl(url);
				bean.setName(name);
				
				if(file != null && !file.getOriginalFilename().equalsIgnoreCase("")){
					bean.setPictureLocation(file.getOriginalFilename());
					bean.setPicture(file.getBytes());
				}
				
				this.slidesService.update(bean);
				response = super.responseSuccess();
				response.setDatas(bean);
			}
		}catch(Exception e){
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
			e.printStackTrace();
		}
		request.setAttribute(SessionConstr.ACTIVE_PAGE, "slides");
		return "redirect:";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value="delete", method=RequestMethod.POST)
	@ResponseBody
	public FintrackResponse<SlidesBean> delete(final HttpServletRequest request, @RequestBody final SlidesBean bean){
		FintrackResponse<SlidesBean> response = super.responseError(MsgConstr.CANNOT_DELETE_DATA_CODE, MsgConstr.CANNOT_DELETE_DATA_MSG);
		try{			
			System.err.println(bean.toString());
			this.slidesService.delete(bean);
			response = super.responseSuccess();
		}catch(Exception e){
			response = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
			e.printStackTrace();
		}
		return response;
	}
}
